﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DataManager
{
    public partial class frmFilmek : Form
    {
        Movies m_Movies;

        public frmFilmek()
        {
            InitializeComponent();
            m_Movies = new Movies();
        }

        private void frmFilmek_Load(object sender, EventArgs e)
        {
            FillDgvMovies();
            SetInputsState(false);
        }

        /// <summary>
        /// Feltolti a DataGridViewt
        /// </summary>
        private void FillDgvMovies()
        {
            dgvMovies.Rows.Clear();
            List<Movie> movieList = m_Movies.GetMovieList("",0);
            foreach (Movie item in movieList)
            {
                dgvMovies.Rows.Add(item.MovieId, item.OriginalTitle, item.HungarianTitle, item.ReleaseDate, item.Price, item.Quantity);
            }
        }

        /// <summary>
        /// Az uj elem bevitelehez illetve letezo elem modositasahoz szukseges elemek elerhetetlenek vagy elerhetoek
        /// </summary>
        /// <param name="state"></param>
        private void SetInputsState(bool state)
        {
            cbNew.Enabled = state;
            tbEredeti.Enabled = state;
            tbMagyar.Enabled = state;
            tbMegjEv.Enabled = state;
            tbAr.Enabled = state;
            tbRakt.Enabled = state;
            btnMent.Enabled = state;
            btnMegse.Enabled = state;
        }

        private void ClearInputValues()
        {
            tbEredeti.Text = "";
            tbMagyar.Text = "";
            tbMegjEv.Text = "";
            tbAr.Text = "";
            tbRakt.Text = ""; 
        }

        private void btnVissza_Click(object sender, EventArgs e)
        {
            frmDVDk myForm = new frmDVDk();
            Hide();
            myForm.ShowDialog();
            Close();
        }

        private void btnKilepes_Click(object sender, EventArgs e)
        {
            Close();
        }

        /// <summary>
        /// A szukit gombra kattintva kiolvassa a keresesi feltetelt es aszerint tolti fel a DataGridViewt
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSzukit_Click(object sender, EventArgs e)
        {
            dgvMovies.Rows.Clear();
            List<Movie> movieList = m_Movies.GetMovieList(tbSzukit.Text, 0);
            foreach (Movie item in movieList)
            {
                dgvMovies.Rows.Add(item.MovieId, item.OriginalTitle, item.HungarianTitle, item.ReleaseDate, item.Price, item.Quantity);
            }
        }

        /// <summary>
        /// Az Uj gombra kattintva elerhetoek az adatok bevitelehez szukseges elemek. 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUj_Click(object sender, EventArgs e)
        {
            SetInputsState(true);
            ClearInputValues();
            cbNew.Checked = true;
        }

        /// <summary>
        /// A Modosit gombra kattintva elerhetoek az adatok modositasahoz szukseges elemek.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnModosit_Click(object sender, EventArgs e)
        {
            if (dgvMovies.SelectedRows.Count != 0)
            {
                SetInputsState(true);
                cbNew.Checked = false;
                tbEredeti.Text = dgvMovies.SelectedRows[0].Cells["EredetiCim"].Value.ToString();
                tbMagyar.Text = dgvMovies.SelectedRows[0].Cells["MagyarCim"].Value.ToString();
                tbMegjEv.Text = dgvMovies.SelectedRows[0].Cells["MegjEv"].Value.ToString();
                tbAr.Text = dgvMovies.SelectedRows[0].Cells["Ar"].Value.ToString();
                tbRakt.Text = dgvMovies.SelectedRows[0].Cells["MennyRakt"].Value.ToString();
            }
        }

        /// <summary>
        /// Torles gombra kattintva torli a kijelolt sort
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTorol_Click(object sender, EventArgs e)
        {
            if (dgvMovies.SelectedRows.Count != 0)
            {
                string error = m_Movies.DeleteMovie(Convert.ToInt32(dgvMovies.SelectedRows[0].Cells["FilmId"].Value));
            }
            FillDgvMovies();
        }

        /// <summary>
        /// A Mentes gombra kattintva elmenti az adatokat, ha a checkbox kivan jelolve akkor uj sort szur be egyebkent modositja a mar meglevot
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMent_Click(object sender, EventArgs e)
        {
            if (cbNew.Checked)
            {
                string error = m_Movies.InsertMovie(tbEredeti.Text, tbMagyar.Text, Convert.ToInt32(tbMegjEv.Text), Convert.ToInt32(tbAr.Text), Convert.ToInt32(tbRakt.Text));
            }
            else
            {
                string error = m_Movies.UpdateMovie(Convert.ToInt32(dgvMovies.SelectedRows[0].Cells["FilmId"].Value), tbEredeti.Text, tbMagyar.Text, Convert.ToInt32(tbMegjEv.Text), Convert.ToInt32(tbAr.Text), Convert.ToInt32(tbRakt.Text));
            }
            ClearInputValues();
            SetInputsState(false);
            FillDgvMovies();
        }

        private void btnMegse_Click(object sender, EventArgs e)
        {
            ClearInputValues();
            SetInputsState(false);
        }

    }
}
